//
//  NavigationBarView.swift
//  zhonglb_iOS
//
//  Created by Rowling on 2021/11/16.
//

import UIKit

class NavigationBarView: UIView {

    //MARK:--------------------属性--------------------
    var autoBack = false
    var disposeBag = DisposeBag()
    
    var rightButtonAction : DisposeBag!
    
    var showLine = false {
        didSet {
            line.isHidden = !showLine
        }
    }
    
    var shouldGoBack : (()->Bool)?
    //MARK:--------------------UI控件--------------------
    var contentView = UIView()
    var backButton = ZMUtil.button(image: UIImage.init(named: "back_18"))
    var titleLabel = ZMUtil.label(title: "", color: UIColor.zm.color333333(), font: UIFont.systemFont(ofSize: 16))
    lazy var rightButton = UIButton()
    var line = UIView().then { v in
        v.backgroundColor = Style.lineColor
        v.isHidden = true
    }
    //MARK:--------------------生命周期--------------------
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
        bindAction()
        addConstraints()
    }
    
    required init?(coder: NSCoder) {
        super.init(coder: coder)
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        setUI()
        bindAction()
        addConstraints()
    }

    //MARK:--------------------UI--------------------
    func setUI() {
        snp.makeConstraints { make in
            make.height.equalTo(STATUS_HEIGHT + 44)
        }
        addSubview(contentView)
        contentView.addSubview(backButton)
        contentView.addSubview(titleLabel)
        backButton.tintColor = .white
        contentView.addSubview(line)
    }
    
    func addConstraints() {
        contentView.snp.makeConstraints { make in
            make.bottom.left.right.equalToSuperview()
            make.height.equalTo(44)
        }
        backButton.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(16)
            make.width.height.equalTo(30)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.left.greaterThanOrEqualTo(backButton.snp.right).offset(8)
        }
        line.snp.makeConstraints { make in
            make.bottom.left.right.equalToSuperview()
            make.height.equalTo(0.5)
        }
    }
    
    func setRightButton(_ block : (_ btn : UIButton) -> Void, _ rightClick :@escaping ()->()) {
        rightButtonAction = DisposeBag()
        contentView.addSubview(rightButton)
        rightButton.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-16)
        }
        block(rightButton)
        
        rightButton.rx.tap.subscribe(onNext:{ resp in
            rightClick()
        }).disposed(by: rightButtonAction)
    }
    
    //MARK:--------------------methods--------------------
    func bindAction() {
        backButton.rx.tap.subscribe(onNext:{ [unowned self] resp in
            if self.autoBack == false {return}
            self.goBack()
        }).disposed(by: disposeBag)
    }
    
    func goBack() {
        if let shouldGoBack = shouldGoBack {
            if shouldGoBack() == false {return}
        }
        var theV : UIView = self
        var vc : UIViewController?
        while theV.superview != nil {
            theV = theV.superview!
            if let nR = theV.next, nR is UIViewController{
               vc = nR as? UIViewController
               break
            }
        }
        vc?.navigationController?.popViewController(animated: true)
    }
    
    func setMainColor(color : UIColor) {
        titleLabel.textColor = color
        backButton.tintColor = color
    }
    //MARK:--------------------delegate--------------------
}

